package com.wbt.bambootown.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.gyf.barlibrary.ImmersionBar;
import com.wbt.bambootown.R;
import com.wbt.bambootown.hk.HC_DVRManager;
import com.wbt.bambootown.utils.EventCenter;
import com.wbt.bambootown.utils.VibrateHelp;

import butterknife.BindView;

public class FullScreenActivity extends BaseActivity implements View.OnTouchListener {

    @BindView(R.id.sf_VideoMonitor_FullScreen)
    SurfaceView sfVideoMonitorFullScreen;
    @BindView(R.id.tv_Loading)
    TextView tvLoading;
    @BindView(R.id.rl_MonitorCtrl)
    RelativeLayout rlMonitorCtrl;
    @BindView(R.id.iv_auto)
    ImageView ivAuto;
    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.iv_top)
    ImageView ivTop;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.iv_down)
    ImageView ivDown;

    /**
     * 正在移动标记
     */
    private boolean isMoving;

    /**
     * 用来判定是否退回了手机桌面，需要重新开始连接播放
     */
    private boolean isNeedConnect;


    private final StartRenderingReceiver receiver = new StartRenderingReceiver();

    @Override
    public void initStatusBar(ImmersionBar immersionBar) {
        immersionBar
                .transparentStatusBar()
                .init();
    }

    @Override
    public void initView() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(HC_DVRManager.ACTION_START_RENDERING);
        filter.addAction(HC_DVRManager.ACTION_DVR_OUTLINE);
        registerReceiver(receiver, filter);
        initHKView();
    }


    @Override
    public void onPause() {
        super.onPause();
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
        HC_DVRManager.getInstance().stopPlay();
        isNeedConnect = true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new Thread() {
                @Override
                public void run() {
                    HC_DVRManager.getInstance().stopPlay();
                }
            }.start();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isNeedConnect) {
            //设置用于发广播的上下文
            HC_DVRManager.getInstance().setContext(this);
            initHKView();
            start();
        }
    }

    private void start() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(HC_DVRManager.ACTION_START_RENDERING);
        filter.addAction(HC_DVRManager.ACTION_DVR_OUTLINE);
        registerReceiver(receiver, filter);

        tvLoading.setVisibility(View.VISIBLE);
        tvLoading.setText(getString(R.string.connecting));

        new Thread() {
            @Override
            public void run() {
                HC_DVRManager.getInstance().setSurfaceHolder(sfVideoMonitorFullScreen.getHolder());
                HC_DVRManager.getInstance().realPlay();
            }
        }.start();


        isNeedConnect = false;
    }

    @Override
    public void initData() {

    }

    @Override
    public Object getContentView() {
        return R.layout.activity_full_screen;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }


    @Override
    public boolean onTouch(final View v, final MotionEvent event) {
        if (isMoving) {
            return true;
        }
        new Thread() {
            @Override
            public void run() {
                switch (v.getId()) {
                    case R.id.iv_top:
                        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                            VibrateHelp.vSimple(getApplicationContext(), 100);
                            HC_DVRManager.getInstance().startMove(8);
                        }
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            HC_DVRManager.getInstance().stopMove(8);
                        }
                        break;
                    case R.id.iv_left:
                        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                            VibrateHelp.vSimple(getApplicationContext(), 100);
                            HC_DVRManager.getInstance().startMove(4);
                        }
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            HC_DVRManager.getInstance().stopMove(4);
                        }
                        break;
                    case R.id.iv_right:
                        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                            VibrateHelp.vSimple(getApplicationContext(), 100);
                            HC_DVRManager.getInstance().startMove(6);
                        }
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            HC_DVRManager.getInstance().stopMove(6);
                        }
                        break;
                    case R.id.iv_down:
                        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                            VibrateHelp.vSimple(getApplicationContext(), 100);
                            HC_DVRManager.getInstance().startMove(2);
                        }
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            HC_DVRManager.getInstance().stopMove(2);
                        }
                        break;
                    default:
                        break;
                }
            }
        }.start();
        return true;
    }

    private class StartRenderingReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (HC_DVRManager.ACTION_START_RENDERING.equals(intent.getAction())) {
                tvLoading.setVisibility(View.GONE);
                rlMonitorCtrl.setVisibility(View.VISIBLE);
            }
            if (HC_DVRManager.ACTION_DVR_OUTLINE.equals(intent.getAction())) {
                tvLoading.setText(getString(R.string.tv_connect_cam_error));
            }
        }
    }

    /**
     * 海康威视相关初始化
     */
    private void initHKView() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        ViewGroup.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT , ViewGroup.LayoutParams.MATCH_PARENT);
        sfVideoMonitorFullScreen.setLayoutParams(lp);
        tvLoading.setLayoutParams(lp);

        sfVideoMonitorFullScreen.getHolder().addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                sfVideoMonitorFullScreen.destroyDrawingCache();
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                new Thread() {
                    @Override
                    public void run() {
                        HC_DVRManager.getInstance().setSurfaceHolder(sfVideoMonitorFullScreen.getHolder());
                        HC_DVRManager.getInstance().initSDK();
                        HC_DVRManager.getInstance().loginDevice();
                        HC_DVRManager.getInstance().realPlay();
                    }
                }.start();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }
        });

        ivLeft.setOnTouchListener(this);
        ivTop.setOnTouchListener(this);
        ivRight.setOnTouchListener(this);
        ivDown.setOnTouchListener(this);

        ivAuto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                new Thread() {
                    @Override
                    public void run() {
                        if (isMoving) {
                            HC_DVRManager.getInstance().stopMove(5);
                            isMoving = false;
                        } else {
                            HC_DVRManager.getInstance().startMove(5);
                            isMoving = true;
                        }
                    }
                }.start();
            }
        });

    }
}
